/**
 *	---------------------------------------------------------------------------
 *	COMP 6231/FALL 2013
 *	Assignment - 02
 *	@author Talha Rahman (1296884)
 *	---------------------------------------------------------------------------
 *	This is a driver class which loads three instances of the RMI server.
 *	@see {@link StationServer}
 *	  
 */

package ca.concordia.comp6231.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import ca.concordia.comp6231.server.StationServer;
import ca.concordia.comp6231.server.StationServerCORBA;

public class UDPService implements Runnable{
	private final int portNo;
	private final StationServerCORBA corbaServer;
	
	private Map<String, Map<String, String>> holdBackQueue = new HashMap<String, Map<String, String>>();
	
	public UDPService(int portNo, StationServer rmiServer) {
		this.portNo = portNo;
		this.corbaServer = null;
	}
	
	public UDPService(int portNo, StationServerCORBA corbaServer) {
		this.portNo = portNo;
		this.corbaServer = corbaServer;
	}

	@Override
	public void run() {
		DatagramSocket aSocket = null;
		try {
			aSocket = new DatagramSocket(portNo);
			
			while (true) {
				byte[] buffer = new byte[1000];
				DatagramPacket request = new DatagramPacket(buffer, buffer.length);
				aSocket.receive(request);
				
				String requestString = new String(request.getData());
				System.out.println(requestString);
				Map<String, String> params = processRequestString(requestString);
				
				if(params.containsKey("orderNo")) {
					holdBackQueue.put(params.get("orderNo"), params);
				}
				
				processRequest(aSocket, request, corbaServer);
				buffer = null;
			}
		} catch (SocketException e) {
			System.out.println("Socket error: " + e.getMessage());
		} catch (IOException e) {
			System.out.println("IO error: " + e.getMessage());
		} finally {
			if (aSocket != null)
				aSocket.close();
		}		
	}
	
	private void processRequest(DatagramSocket socket, DatagramPacket request, StationServerCORBA corbaServer){
			
			UDPServiceRequestProcessor requestProcessorThread 
						= new UDPServiceRequestProcessor(corbaServer, socket, request);
			requestProcessorThread.start();
	}
	
	private Map<String, String> processRequestString(String requestStringRaw) {
		String requestString = requestStringRaw.trim();
		
		System.out.println("Processing string:" + requestString);
		
		List<String> items = Arrays.asList(requestString.trim().split("\\,"));
		Map<String, String> params = new HashMap<String, String>();
		
		System.out.println(items);
		
		for(String item : items) {
			List<String> parts = Arrays.asList(item.trim().split("\\:"));
			String key = parts.get(0);
			String value = parts.get(1);
			params.put(key, value);
		}
		
		return params;
	}
}
